package com.benjaminwan.read.free.config

import com.benjaminwan.read.free.config.Options.getAction
import com.benjaminwan.read.free.model.ItemConfig
import com.benjaminwan.read.free.model.action.MouseMoveLong
import com.benjaminwan.read.free.model.action.Position
import com.benjaminwan.read.free.model.enums.ItemType
import com.benjaminwan.read.free.model.listOfItemConfig
import com.benjaminwan.read.free.ui.main.MainViewModel
import com.benjaminwan.read.free.utils.*
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView

object MouseMoveLongConfig {
    fun getMouseMoveLongConfig(mainVM: MainViewModel, pos: Position): List<ItemConfig> {
        return listOfItemConfig {
            itemConfig {
                type = ItemType.Text
                titleStr = "x轴移动距离"
                rightTextFunc = { getAction<MouseMoveLong>(mainVM, pos)?.dx?.toString() ?: "" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 1, 0, getRegexFilter(numRegex, "[-]"),
                        { dialog, which ->
                            val default = "0"
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<MouseMoveLong>(mainVM, pos) ?: return@showRightTextInputDialog
                        val delta = input.toInt()
                        mainVM.setAction(pos, action.copy(dx = delta))
                        dialog.dismiss()
                    }
                }
            }

            itemConfig {
                type = ItemType.Text
                titleStr = "y轴移动距离"
                rightTextFunc = { getAction<MouseMoveLong>(mainVM, pos)?.dy?.toString() ?: "" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 1, 0, getRegexFilter(numRegex, "[-]"),
                        { dialog, which ->
                            val default = "0"
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<MouseMoveLong>(mainVM, pos) ?: return@showRightTextInputDialog
                        val delta = input.toInt()
                        mainVM.setAction(pos, action.copy(dy = delta))
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "x步进"
                rightTextFunc = { "${getAction<MouseMoveLong>(mainVM, pos)?.stepXRange ?: ""}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, getRegexFilter(numRegex, "[.]+"),
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<MouseMoveLong>(mainVM, pos) ?: return@showRightTextInputDialog
                        if (input.isEmpty()) {
                            mainVM.setAction(pos, action.copy(stepXRange = null))
                        } else if (!input.contains("..")) {
                            dialog.view.shakeReverse()
                            view.context.showXToastError("输入格式错误")
                            return@showRightTextInputDialog
                        } else {
                            val range = input.split("..")
                            if (range.isEmpty() || range.size != 2) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误")
                                return@showRightTextInputDialog
                            }
                            val start = range[0].toIntOrNull()
                            val end = range[1].toIntOrNull()
                            if (start == null || end == null) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误:数字转换错误")
                                return@showRightTextInputDialog
                            }
                            if (start > end) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)>结束($end)")
                                return@showRightTextInputDialog
                            }
                            if (start == 0 && end == 0) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,不允许起始($start)=0和结束($end)=0")
                                return@showRightTextInputDialog
                            }
                            mainVM.setAction(pos, action.copy(stepXRange = (start..end)))
                        }
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "y步进"
                rightTextFunc = { "${getAction<MouseMoveLong>(mainVM, pos)?.stepYRange ?: ""}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, getRegexFilter(numRegex, "[.]+"),
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<MouseMoveLong>(mainVM, pos) ?: return@showRightTextInputDialog
                        if (input.isEmpty()) {
                            mainVM.setAction(pos, action.copy(stepYRange = null))
                        } else if (!input.contains("..")) {
                            dialog.view.shakeReverse()
                            view.context.showXToastError("输入格式错误")
                            return@showRightTextInputDialog
                        } else {
                            val range = input.split("..")
                            if (range.isEmpty() || range.size != 2) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误")
                                return@showRightTextInputDialog
                            }
                            val start = range[0].toIntOrNull()
                            val end = range[1].toIntOrNull()
                            if (start == null || end == null) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误:数字转换错误")
                                return@showRightTextInputDialog
                            }
                            if (start > end) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)>结束($end)")
                                return@showRightTextInputDialog
                            }
                            if (start == 0 && end == 0) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,不允许起始($start)=0和结束($end)=0")
                                return@showRightTextInputDialog
                            }
                            mainVM.setAction(pos, action.copy(stepYRange = (start..end)))
                        }
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "步进之间延时"
                rightTextFunc = { "${getAction<MouseMoveLong>(mainVM, pos)?.delayRange ?: ""}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, getRegexFilter(numRegex, "[.]+"),
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<MouseMoveLong>(mainVM, pos) ?: return@showRightTextInputDialog
                        if (input.isEmpty()) {
                            mainVM.setAction(pos, action.copy(delayRange = (1L..1)))
                        } else if (!input.contains("..")) {
                            dialog.view.shakeReverse()
                            view.context.showXToastError("输入格式错误")
                            return@showRightTextInputDialog
                        } else {
                            val range = input.split("..")
                            if (range.isEmpty() || range.size != 2) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误")
                                return@showRightTextInputDialog
                            }
                            val start = range[0].toLongOrNull()
                            val end = range[1].toLongOrNull()
                            if (start == null || end == null) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误:数字转换错误")
                                return@showRightTextInputDialog
                            }
                            if (start > end) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)>结束($end)")
                                return@showRightTextInputDialog
                            }
                            if (start <= 0L) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)<=0")
                                return@showRightTextInputDialog
                            }
                            mainVM.setAction(pos, action.copy(delayRange = (start..end)))
                        }
                        dialog.dismiss()
                    }
                }
            }

        }
    }
}